package com.cy.controller;


import com.cy.bfzcommons.pojo.AdminPassword;
import com.cy.bfzcommons.pojo.Administrator;
import com.cy.bfzcommons.service.AdministratorService;
import com.cy.bfzcommons.web.util.JsonResult;
import com.cy.bfzcommons.web.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController

@CrossOrigin
@RequestMapping("/administrator")

@Api(
        value = "/cart",description = "AdministratorController"
        ,tags = "管理员模块"
)
//@RequiresPermissions("bfz:admin:list")
public class AdministratorController {

    @Autowired
    private AdministratorService administratorService;

    /**
     * 新增管理员   新增中间表给管理员赋予权限
     * @return
     */

    @PostMapping
    public JsonResult saveAdmin(@RequestBody Administrator administrator){

        return JsonResult.ok(administratorService.saveAdmin(administrator));
    }

    /**
     * 用户自身修改信息     以及用于   超级管理员修改该用户状态   并且修改中间表
     * @param administrator
     * @return
     */

    @PutMapping
    public JsonResult updateAdmin(@RequestBody Administrator administrator){
        administratorService.updateAdmin(administrator);
        return JsonResult.ok();
    }

    /**
     * 查询所有  或者 根据管理员名查询
     * @param username
     * @return
     */


    @GetMapping
    public JsonResult findAdmin(String username){
       return JsonResult.ok( PageUtil.startpage().doSelectPageInfo(()->
                administratorService.findAdmin(username)));

    }


    /**
     * 根据id查询用户信息
     * @param administratorId
     * @return
     */
    @GetMapping("/selectById/{administratorId}")
    public JsonResult selectById(@PathVariable Integer administratorId){
         Administrator administrator = administratorService.selectById(administratorId);
        System.out.println("让我看看:"+administratorId);
        return JsonResult.ok(administrator);
    }


    /**
     * 修改密码
     * @param
     * @return
     */
    @PutMapping("/password")
    public JsonResult updatePassword(@RequestBody AdminPassword adminPassword){
        System.out.println(adminPassword.getPassword());
        System.out.println(adminPassword.getNewPassword());
        return JsonResult.ok(administratorService.updatePassword(adminPassword));
    }


//    @PostMapping("/login/{userName}/{password}")
//    public JsonResult loginAdministrator(@PathVariable String userName, @ApiParam(name="password",value="管理员密码",required=true)@PathVariable String password){
//        //将账号和密码封装token对象
//        UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
//        //基于subject对象将token提交给securityManager
//        token.setRememberMe(true);//设置记住我的功能
//        Subject subject = SecurityUtils.getSubject();
//        subject.login(token);//提交给securityManager
//
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setData(userName);
//        jsonResult.setStatus(200);
//        jsonResult.setMessage("登录成功");
//
//       // administratorService.loginAdministrator(userName,password);
//        return jsonResult;
//
//    }

    @GetMapping("/username")
    public JsonResult selectUserName(@ApiParam(name="username",value="管理员名",required=true) String username){
        Administrator administrator =  administratorService.selectUsername(username);
        return JsonResult.ok(administrator);
    }



}
